from typing import List


def threeSum(nums: List[int]) -> List[List[int]]:
    ans = []
    nums.sort()
    n = len(nums)
    if n == 0:
        return ans
    for i in range(n):
        if i > 0 and nums[i] == nums[i - 1]:
            continue
        left, right = i + 1, n - 1
        while left < right:
            if left > i + 1 and nums[left] == nums[left - 1]:
                left += 1
                continue
            while left < right - 1 and nums[i] + nums[left] + nums[right-1] >= 0:
                right -= 1
            if nums[i] + nums[right] + nums[left] == 0:
                ans.append([nums[i], nums[left], nums[right]])
            left += 1
    return ans


print(threeSum([-1,0,1,2,-1,-4]))